package com.wuyan.locationdog.view

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.animation.Animation
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.ShareTimeListAdapter
import com.arcsoft.commonbase.bean.ShareTimeBean
import com.arcsoft.commonbase.bean.TeamDetail
import com.wuyan.locationdog.adapter.TeamListAdapter
import com.wuyan.locationdog.adapter.TeamListSelectAdapter
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.Direction
import razerdp.util.animation.TranslationConfig


class TeamListPopup(context: Context?, var teamList: MutableList<TeamDetail>?, var teamId: (String) -> Unit) : BasePopupWindow(context) {

    private val mAdapter by lazy { TeamListSelectAdapter(R.layout.item_team_select, null) }


    init {
        contentView = createPopupById(R.layout.pop_share)
        popupGravity = Gravity.BOTTOM
        setBackPressEnable(false)
        setOutSideDismiss(false)
        isOutSideTouchable = false
    }

    override fun onCreateShowAnimation(): Animation? {
        return AnimationHelper.asAnimation()
            .withTranslation(
                TranslationConfig()
                    .from(Direction.BOTTOM)
            )
            .toShow()
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        findViewById<TextView>(R.id.btnSure).setOnClickListener {
            teamId.invoke(mAdapter.data.find { it.isSelect }?.teamId ?: "")
            dismiss()
        }
        findViewById<TextView>(R.id.btnCancel).setOnClickListener {
            mAdapter.data.find { it.isSelect }?.isSelect = false
            teamId.invoke("")
            dismiss()
        }


        val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
        recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter.apply {
                setNewInstance(teamList)


                setOnItemClickListener { adapter, view, position ->
                    data.find { it.isSelect }?.isSelect = false
                    data[position].isSelect = true
                    notifyDataSetChanged()
                }
            }
        }
    }
}
